/*
 * @Description: 授权接口
 * @Author: chendingfengmail@163.com
 * @Date: 2021-11-03 15:38:18
 * @LastEditTime: 2023-02-14 18:53:01
 * @LastEditors: luozhenfu
 * @Reference:
 */
import { request } from '../service'
import util from '@/libs/util'

const oauth = {
	/**
	* 获取登录安全码
	* @param {Boolean} text      - 是否获取文本类型安全码
	* @param {String}  clientId  - 客户端标识
	* @returns {String}          - 登录安全码
	*/
	getCheckCode({ text, clientId }) {
		if (clientId == null || clientId == undefined) {
			clientId = util.createUniqueString()
			util.cookies.set('clientId', clientId)
		}
		return request({
			url: '/auth/oauth/getCode',
			method: 'GET',
			params: { text, clientId }
		})
	},

	/**
	* 获取手机验证码
	* @param {String} cellphone
	* @returns
	*/
	getCode({ cellphone, clientId }) {
		if (clientId == null || clientId == undefined) {
			clientId = util.createUniqueString()
			util.cookies.set('clientId', clientId)
		}
		let param = {
			'cellphone': cellphone,
			'text': true,
			'verify': true,
			'clientId': clientId
		}
		return request({
			url: '/auth/oauth/getCode',
			method: 'GET',
			params: param
		})
	},

	/**
	 * 用户注册
	 * @param {String} username
	 * @param {String} password
	 * @param {String} mobile
	 * @param {String} clientId
	 * @param {String} userType
	 * @param {String} relativeId
	 * @param {String} relativeName
	 * @param {String} roles 多个角色逗号隔开：admin,enterprise
	 * @returns
	 */
	register({ name, username, password, mobile, clientId, userType, relativeId, relativeName, roles }) {
	  let param = {
		'name': name,
		'username': username,
		'password': util.getmd5(password + process.env.VUE_APP_MD5_SALT_VALUE),
		'mobile': mobile,
		'client_id': 'dondown_client_id',
		'client_secret': util.getmd5('dondown_client_secret' + process.env.VUE_APP_MD5_SALT_VALUE),
		'clientId': clientId,
		'userType': userType,
		'relativeId': relativeId,
		'relativeName': relativeName,
		'roles': roles,
	  }
	  return request({
		url: '/auth/oauth/register',
		method: 'POST',
		params: param
	  })
	},

	/**
	 * 获取 token 信息
	 * @param {String} username - 用户名
	 * @param {String} password - 用户密码
	 * @param {String} code     - 验证码
	 * @param {String} clientId - 客户端标识
	 * @returns {Object}        - access_token  令牌
	 *                          - refresh_token 刷新令牌
	 *                          - expire_time   过期时间（s）
	 *                          - token_type    bearer
	 */
	getToken({ username, password, code, clientId, loginType, client_id, client_secret }) {
		if(password != null && password != undefined){
      password = util.getmd5(password + process.env.VUE_APP_MD5_SALT_VALUE);
    }
	  let url = '/auth/oauth/token';
	  let param = {
		'username': username,
		'password': password,
		'code': code,
		'loginType': loginType,       // 标识为用户名密码登录模式
		'client_id': 'dondown_client_id',
		'client_secret': util.getmd5('dondown_client_secret' + process.env.VUE_APP_MD5_SALT_VALUE),
		'grant_type': 'password',
		'clientId': clientId
	  };
	  return request({
		url: url,
		method: 'POST',
		params: param
	  })
	},

	/**
	 * 超级管理员以某用户身份进行登录认证
	 * @param {String} proxyUser - 被代理的账号名称
	 * @returns {Object}        - access_token  令牌
	 *                          - refresh_token 刷新令牌
	 *                          - expire_time   过期时间（s）
	 *                          - token_type    bearer
	 */
	loginAs(proxyUser) {
	  let url = '/auth/oauth/loginAs';
	  let param = {
		'proxyUser': proxyUser
	  };
	  return request({
		url: url,
		method: 'GET',
		params: param
	  })
	},

	/**
	 * 刷新令牌
	 * @param {String} refresh_token - 登陆时换取的刷新令牌
	 * @returns
	 */
	refresh(refresh_token) {
	  let param = {
		'refresh_token': refresh_token,
		'client_id': 'dondown_client_id',
		'client_secret': util.getmd5('dondown_client_secret' + process.env.VUE_APP_MD5_SALT_VALUE),
		'grant_type': 'refresh_token'
	  };
	  return request({
		url: '/auth/oauth/token',
		method: 'post',
		params: param
	  });
	},

	/**
	 * 注销
	 * @returns {void}
	 */
	logout() {
	  return request({
		url: '/auth/oauth/logout',
		method: 'post'
	  })
	},
}

export default oauth;
